<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="24" :xs="24">
        <el-form
          v-show="showSearch"
          ref="queryForm"
          :model="queryParams"
          :inline="true"
          label-width="100px"
        >
          <el-form-item label="审核状态" prop="status">
            <el-select
              v-model="queryParams.status"
              placeholder="审核状态"
              clearable
              style="width: 240px"
            >
              <el-option :key="0" label="未审核" :value="0"/>
              <el-option :key="1" label="已审核" :value="1"/>
              <el-option :key="2" label="驳回" :value="2"/>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-col :span="20">
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
            >搜索
            </el-button>
            </el-col>
          </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
            >新增</el-button>
          </el-col>
        </el-row>

        <el-table :data="leaveInfoList" style="width: 100%" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55"/>
          <el-table-column v-if="false" prop="id" label="id" align="center" hidden/>
          <el-table-column prop="startTime" label="请假开始时间" align="center"/>
          <el-table-column prop="endTime" label="请假结束时间" align="center"/>
          <el-table-column prop="reason" label="请假原因" align="center">
          </el-table-column>
          <el-table-column prop="status" label="是否审批" align="center">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.status == '未审批'" >未审核</el-tag>
              <el-tag v-if="scope.row.status == '已审批'" type="success">已审批</el-tag>
              <el-tag v-if="scope.row.status == '已驳回'" type="danger">驳回</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="rejectReason" label="驳回原因" align="center"/>

          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template v-if="scope.row.status =='未审批'" slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handle(scope.row)"
              >修改
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <el-pagination
      :page-size.sync="queryParams.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      :page-sizes="[10, 20, 30, 40]"
      :current-page.sync="queryParams.pageNum"
      @current-change="getList"
      @size-change="getList"
    />
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-row>
          <el-col :span="24">


            <el-form-item label="请假开始时间：">
              <el-col :span="11">
                <el-date-picker type="date" style="width: 400px" value-format="yyyy-MM-dd"  @change="changeDate()" :picker-options="pickerOptions" placeholder="选择日期" v-model="form.startTime" ></el-date-picker>
              </el-col>
            </el-form-item>

            <el-form-item label="请假结束时间：">
              <el-col :span="11">
                <el-date-picker type="date"  style="width: 400px" value-format="yyyy-MM-dd"  :picker-options="pickerOptions" placeholder="选择日期" v-model="form.endTime"></el-date-picker>
              </el-col>
            </el-form-item>

            <el-form-item label="请假原因" prop="title">
              <el-input
                v-model="form.reason"
                placeholder="请输入请假原因"
                style="width: 400px"
              />
            </el-form-item>

          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {addLeaveInfo, updateLeaveInfo, getDoctorLeaveInfo, getLeaveInfoList} from "@/api/leave/leave";

export default {
  name: 'User',
  data() {
    let self = this;
    return {
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: undefined
      },
      title: '',
      // 是否显示弹出层
      open: false,
      // 表单校验
      rules: {
        reason: [
          {required: true, trigger: 'blur', message: '驳回原因不能为空'}
        ]
      },

      // 显示搜索条件
      showSearch: true,
      // 遮罩层
      loading: true,
      // 用户表格数据
      leaveList: [],
      leaveInfoList: [],
      doctorList: null,
      // 总条数
      total: 0,
      // 选中数组
      ids: [],
      // 表单参数
      form: {},
      reason:undefined,
      pickerOptions: {
        disabledDate(time) {
          let dayOfWeek = time.getDay();
          if(self.compare(time.getTime(),Date.now())){
            return time.getTime() < Date.now();
          }else if(dayOfWeek === 0 || dayOfWeek === 6){
            // 如果是周六或周日，禁用该日期
            return true;
          }else if(self.compareLeave(time)){
            return true;
          }
          return time.getTime() < Date.now() - 8.64e7;
        },
      }
    }
  },
  watch: {},
  created() {
    this.getList()
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加请假信息'
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    dateToString(dateTime){
      dateTime = new Date(dateTime)
      // 获取年份
      let Y = dateTime.getFullYear() + '-'
      //获取当前年的月份 月份要 + 1 （0代表1月）date.getMonth() + 1
      let M = dateTime.getMonth() + 1 < 10 ? '0' + (dateTime.getMonth() + 1) + '-' : (dateTime.getMonth() + 1) +'-'
      //获取当前日（1 - 31）
      let D = dateTime.getDate() < 10 ? '0' + (dateTime.getDate())  : (dateTime.getDate()) ;

      return  Y + M + D;
    },
    compare(date,date1){
      let dateStr = this.dateToString(date);
      let dateStr1 = this.dateToString(date1);
      if(dateStr === dateStr1){
        if(new Date(date1).getHours() > 18){
          return true;
        }
      }
    },
    changeDate(){
      console.log(this.form.startTime+" 0:0:0")
      var time = new Date(this.form.startTime+" 0:0:0");
      console.log(time.getTime())
      this.leaveList.push(time.getTime())
    },
    compareLeave(time){
      for (let i = 0 ; i < this.leaveList.length ; i++){
        if(time.getTime() == this.leaveList[i]){
          return true;
        }
      }
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },

    getList() {
      this.loading = true
      getDoctorLeaveInfo(this.queryParams).then((response) => {
        this.leaveList = response
      })
      getLeaveInfoList(this.queryParams).then((response) => {
        this.leaveInfoList = response.list
        this.total = response.total
        this.loading = false
      })
    },

    // 表单重置
    reset() {
      this.reason =undefined
    },

    handle(row) {
      this.reset()
      this.form = row
      this.open = true
      this.title = '修改请假信息'
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id !== undefined) {
            updateLeaveInfo(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList();
            })
          }else{
            addLeaveInfo(this.form).then((response) => {
              this.$modal.msgSuccess('添加成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    }
  }
}
</script>
